<?php
/**
 * @package me.joshua.proj4j.baehelper
 * 
 * BAE Helper option page
 */

/**
 * Create menu item in admin page.
 */
function bae_helper_create_menu() {
	// Add a menu item
	add_options_page('BAE Helper Settings', 'BAE Helper', 'manage_options', 
			'bae_helper_settings', 'bae_helper_settings_page');

	// Register the option function
	add_action('admin_init', 'bae_helper_register_settings');
}
add_action('admin_menu', 'bae_helper_create_menu');

/**
 * Function used to register settings.
 */
function bae_helper_register_settings() {
	register_setting('bae-helper-settings-group', 'bae-helper-settings', 
			'bae_helper_settings_callback');
	add_settings_section('bae-helper-bcms-section', 'BCMS配置', 
			'bae_helper_bcms_section', 'bae-helper');
	add_settings_field('bae-helper-bcms-queue-name', 'BCMS队列名', 
			'bae_helper_bcms_queue_name_field', 'bae-helper', 'bae-helper-bcms-section');
	add_settings_section('bae-helper-page-section', '页面配置', 
			'bae_helper_page_section', 'bae-helper');
	add_settings_field('bae-helper-custom-head', '自定义Head', 
			'bae_helper_custom_head_field', 'bae-helper', 'bae-helper-page-section');
	add_settings_field('bae-helper-custom-footer', '自定义Footer', 
			'bae_helper_custom_footer_field', 'bae-helper', 'bae-helper-page-section');
	add_settings_section('bae-helper-gavatar-section', 'Gravatar配置', 
			'bae_helper_gavatar_section', 'bae-helper');
	add_settings_field('bae-helper-gavatar-proxy-enable', 'Gravatar代理', 
			'bae_helper_gavatar_proxy_enable_field', 'bae-helper', 'bae-helper-gavatar-section');
}

function bae_helper_settings_callback($input) {
	$settings = bae_helper_bcms_get_options();
	
	$bcms_queue_name = trim($input['bcms-queue-name']);
	if (!empty($bcms_queue_name)) {
		if (preg_match('/^[\da-f]{32}$/i', $bcms_queue_name)) {
			$settings['bcms-queue-name'] = $bcms_queue_name;
		} else {
			add_settings_error('bae-helper-settings', 'bae-helper-invalid-bcms-queue-name', 
					'不合法的BCMS队列名，合法的队列名为32位16进制的字符串，如“0123456789ABCDEF0123456789ABCDEF”');
		}
	}
	
	try {
		bae_helper_debug(1 == $input['page-custom-head-enable']);
	} catch (Exception $e) {
		bae_helper_debug('Caught exception: ',  $e->getMessage());
	}
	
	$settings['page-custom-head-enable'] = isset($input['page-custom-head-enable']) ? 1 : 0;
	$page_custom_header = trim($input['page-custom-head']);
	if (!empty($page_custom_header)) {
		$settings['page-custom-head'] = $page_custom_header;
	}
	
	$settings['page-custom-footer-enable'] = isset($input['page-custom-footer-enable']) ? 1 : 0;
	$page_custom_footer = trim($input['page-custom-footer']);
	if (!empty($page_custom_footer)) {
		$settings['page-custom-footer'] = $page_custom_footer;
	}
	
	$settings['gavatar-proxy-enable'] = isset($input['gavatar-proxy-enable']) ? 1 : 0;
	
	return $settings;
}

function bae_helper_bcms_section() {
	?>
	<p>
		使用邮件功能需要申请BCMS队列，<a href="http://developer.baidu.com/bae/bms/list/">申请地址</a>。
	</p>
	<?php
}

function bae_helper_bcms_queue_name_field() {
	$settings = bae_helper_bcms_get_options();
	?>
	<input id="bcms-queue-name" name="bae-helper-settings[bcms-queue-name]" type="text" 
			value="<?php echo esc_attr($settings['bcms-queue-name']); ?>" maxlength="32" size="32" />
	<p class="description">
		队列的名称是一个32位的MD5值，不是可自定义的显示名称，
		<a id="bcms-queue-name-demo-trigger" href="javascript:;" 
				style="text-decoration: none; width: 600px;" >点击显示示例</a>。
		<div id="bcms-queue-name-demo">
			<img src="<?php echo plugins_url('img/bcms-queue-name.png', __FILE__); ?>"/>
		</div>
	</p>
	<?php
}

function bae_helper_page_section() {
	?>
	<p class="description">
		自定义页面Head和Footer内容，便于第三方代码的引入，如：
	</p>
	<ol class="description">
		<li>
			往<a href="https://www.google.com/webmasters/">Google Webmasters</a>、
			<a href="http://zhanzhang.baidu.com/">百度站长平台</a>注册站点时，用于网站验证的Meta标签；
		</li>
		<li>
			添加<a href="http://www.google.com/analytics/">Google</a>、
			<a href="http://zhanzhang.cnzz.com/">CNZZ</a>提供的访问统计JS代码；
		</li>
		<li>
			用于分享内容的<a href="http://share.baidu.com/">百度分享</a>JS代码，等等。
		</li>
	</ol>
	<?php
}

function bae_helper_custom_head_field() {
	$settings = bae_helper_bcms_get_options();
	?>
	<fieldset>
		<legend class="screen-reader-text"><span>自定义Head</span></legend>
		<label for="page-custom-head-enable">
			<input id="page-custom-head-enable" class="page-custom-enable-trigger"
					name="bae-helper-settings[page-custom-head-enable]" 
					type="checkbox" value="1" data-label-id="#page-custom-head-label"
					<?php checked(1 == $settings['page-custom-head-enable'], TRUE); ?>/>
			启用
		</label>
		<br />
		<label id="page-custom-head-label" for="page-custom-head">
			自定义Head内容：<br />
			<textarea id="page-custom-head" name="bae-helper-settings[page-custom-head]" 
					rows="8" cols="60"><?php echo esc_attr($settings['page-custom-head']) ?></textarea>
		</label>
	</fieldset>
	<?php
}

function bae_helper_custom_footer_field() {
	$settings = bae_helper_bcms_get_options();
	?>
	<fieldset>
		<legend class="screen-reader-text"><span>自定义Footer</span></legend>
		<label for="page-custom-footer-enable">
			<input id="page-custom-footer-enable" class="page-custom-enable-trigger"
					name="bae-helper-settings[page-custom-footer-enable]" 
					type="checkbox" value="1" data-label-id="#page-custom-footer-label"
					<?php checked(1 == $settings['page-custom-footer-enable'], TRUE); ?> />
			启用
		</label>
		<br />
		<label id="page-custom-footer-label" for="page-custom-footer">
			自定义Footer内容：<br />
			<textarea id="page-custom-footer" name="bae-helper-settings[page-custom-footer]" 
					rows="8" cols="60"><?php echo esc_attr($settings['page-custom-footer']) ?></textarea>
		</label>
	</fieldset>
	<?php
}

function bae_helper_gavatar_section() {
	?>
	<p class="description">
		由于众所周知的原因，Gravatar的图片服务器难以访问。启用此选项可以使用<a href="http://duoshuo.com/">多说</a>提供的Gravatar代理来访问头像。需要注意的是通过代理，头像的更新可能会有延时。
	</p>
	<?php
}

function bae_helper_gavatar_proxy_enable_field() {
	$settings = bae_helper_bcms_get_options();
	?>
	<label for="gavatar-proxy-enable">
		<input id="gavatar-proxy-enable" 
				name="bae-helper-settings[gavatar-proxy-enable]" type="checkbox" value="1" 
				<?php checked(1 == $settings['gavatar-proxy-enable'], TRUE); ?>/>
		启用
	</label>
	<?php
}

/**
 * Genarate the settings page content.
 */
function bae_helper_settings_page() {
	?>
	<div class="wrap">
		<div class="icon32" id="icon-options-general"><br></div>
		<h2>BAE Helper Settings</h2>
		<form method="post" action="options.php">
			<?php settings_fields('bae-helper-settings-group'); ?>
			<?php do_settings_sections('bae-helper'); ?>
			<?php submit_button(); ?>
		</form>
	</div>
	<script type="text/javascript">
		jQuery(document).ready(function() {
			var demo = jQuery('#bcms-queue-name-demo');
			demo.hide();
			var demoTrigger = jQuery('#bcms-queue-name-demo-trigger');
			demoTrigger.click(function(event) {
				var triggerClass = 'bae-helper-show';
				event.preventDefault();
				if (demo.hasClass(triggerClass)) {
					demo.slideUp('slow');
					demo.removeClass(triggerClass);
					demoTrigger.html('点击显示示例');
				} else {
					demo.slideDown('slow');
					demo.addClass(triggerClass);
					demoTrigger.html('点击隐藏示例');
				}
			});
			
			var customEnable = jQuery('.page-custom-enable-trigger');
			customEnable.each(function() {
				var thisObj = jQuery(this);
				if (!thisObj.is(':checked')) {
					jQuery(thisObj.data('label-id')).hide();
				}
			});
			
			customEnable.change(function() {
				var thisObj = jQuery(this);
				var label = jQuery(thisObj.data('label-id'));
				if (thisObj.is(':checked')) {
					label.slideDown('slow');
				} else {
					label.slideUp('slow');
				}
			});
		});
	</script>
	<?php
}
?>